function varargout = outerui(varargin)
% OUTERUI MATLAB code for outerui.fig
%      OUTERUI, by itself, creates a new OUTERUI or raises the existing
%      singleton*.
%
%      H = OUTERUI returns the handle to a new OUTERUI or the handle to
%      the existing singleton*.
%
%      OUTERUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in OUTERUI.M with the given input arguments.
%
%      OUTERUI('Property','Value',...) creates a new OUTERUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before outerui_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to outerui_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help outerui

% Last Modified by GUIDE v2.5 30-Oct-2012 19:43:00

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @outerui_OpeningFcn, ...
                   'gui_OutputFcn',  @outerui_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before outerui is made visible.
function outerui_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to outerui (see VARARGIN)

% Choose default command line output for outerui
handles.output = hObject;
addpath(genpath('hough_transform'));

addpath(genpath('hough_transform'));

% Update handles structure
guidata(hObject, handles);

addpath(genpath('hough_transform'))

input_folder = getappdata(0,'input_folder');
pre_built_binary_path = getappdata(0,'pre_built_binary_path');

set(handles.edit2,'String',input_folder);
set(handles.edit3,'String',pre_built_binary_path);


% --- Outputs from this function are returned to the command line.
function varargout = outerui_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in buildbutton.
function buildbutton_Callback(hObject, eventdata, handles)
% hObject    handle to buildbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
input_folder = get(handles.edit2,'String');

set(handles.buildbutton,'Enable','off');
set(handles.adjustbutton,'Enable','off');
set(handles.analyzebutton,'Enable','off');

% Perform eyelid extraction
run_eyelid(input_folder);

set(handles.buildbutton,'Enable','on');
set(handles.adjustbutton,'Enable','on');
set(handles.analyzebutton,'Enable','on');

% Automatically fill out the pre_built path
set(handles.edit3,'String',sprintf('%s.mat',input_folder));


% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over buildbutton.
function buildbutton_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to buildbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in adjustbutton.
function adjustbutton_Callback(hObject, eventdata, handles)
% hObject    handle to adjustbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Check whether the input_folder and the output_folder aren't empty
input_folder = get(handles.edit2,'String');
pre_built_binary_path = get(handles.edit3,'String');
setappdata(0,'input_folder',input_folder);
setappdata(0,'pre_built_binary_path',pre_built_binary_path);

% Check whether the pre_built_binary_path is valid
annotation_gui;
close(handles.output);

% --- Executes on button press in analyzebutton.
function analyzebutton_Callback(hObject, eventdata, handles)
input_folder = getappdata(0,'input_folder');
pre_built_binary_path = getappdata(0,'pre_built_binary_path');
if isempty(pre_built_binary_path)
    pre_built_binary_path = get(handles.edit3,'String');
end

if isempty(input_folder)
    input_folder = get(handles.edit2,'String');
end
analyze(pre_built_binary_path,input_folder);




function edit2_Callback(hObject, eventdata, handles)
% hObject    handle to edit2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit2 as text
%        str2double(get(hObject,'String')) returns contents of edit2 as a double


% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit3_Callback(hObject, eventdata, handles)
% hObject    handle to edit3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit3 as text
%        str2double(get(hObject,'String')) returns contents of edit3 as a double


% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
folder_name = uigetdir;
set(handles.edit2,'String',folder_name);

% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
file_name = uigetfile({'*.mat','Select a normal binary'});
set(handles.edit3,'String',file_name);

% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over pushbutton4.
function pushbutton4_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to pushbutton4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
